package com.geek;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RxJava2Demo {

    public static void main1(String[] args) {
        Disposable d = Observable.create((ObservableOnSubscribe<String>) emitter -> {
            emitter.onNext("Hello ");
            emitter.onNext("World!");
            emitter.onComplete();
        }).subscribe(
                s -> log.info("onNext({})", s),
                throwable -> log.info("onError({})", throwable),
                () -> log.info("onComplete()"),
                disposable -> log.info("onSubscribe({})", disposable));
    }

    public static void main(String[] args) {
//        Observable.just("你说", "你的心", "不再", "温热", "如昔").subscribe(new GenericalObserver<>());
        Observable.fromCallable(() -> "你说" + "你的心" + "不再" + "温热" + "如昔").subscribe(new GenericalObserver<>());
    }
}

@Slf4j
class GenericalObserver<T> implements Observer<T> {

    @Override
    public void onSubscribe(Disposable d) {
        log.info("onSubscribe( {} )", d);
    }

    @Override
    public void onNext(T t) {
        log.info("onNext( {} )", t);
    }

    @Override
    public void onError(Throwable e) {
        log.info("onError( {} )", e);
    }

    @Override
    public void onComplete() {
        log.info("onComplete()");
    }
}
